﻿#include "FrostTiger.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../Settings.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    FrostTiger::FrostTiger(MonsterInfo *info) : MonsterObject(info)
    {
        SitDownTime = NewSitDownTime();
    }

    bool FrostTiger::getSitting() const
    {
        return _sitting;
    }

    void FrostTiger::setSitting(bool value)
    {
        if (_sitting == value)
        {
            return;
        }
        _sitting = value;
        setHidden(value);
        S::ObjectSitDown *tempVar = new S::ObjectSitDown(), *CurrentLocation;
        tempVar->ObjectID = ObjectID;
        tempVar->Location = CurrentLocation;
        tempVar->Direction = getDirection();
        tempVar->Sitting = value;
        getCurrentMap()->Broadcast(tempVar, CurrentLocation);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    bool FrostTiger::getCanAttack() const
    {
        return !getSitting() && MonsterObject::getCanAttack();
    }

    bool FrostTiger::getCanMove() const
    {
        return !getSitting() && MonsterObject::getCanMove();
    }

    bool FrostTiger::Walk(MirDirection dir)
    {
        return !getSitting() && MonsterObject::Walk(dir);
    }

    bool FrostTiger::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    long long FrostTiger::NewSitDownTime()
    {
        long long newtime = getEnvir()->getTime() + getEnvir()->Random->Next(1000 * 60 * 2);
        return newtime < SitDownTime ? SitDownTime : newtime;
    }

    void FrostTiger::FindTarget()
    {
    }

    void FrostTiger::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (!ranged)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->TargetID = getTarget()->ObjectID;
            Broadcast(tempVar2);
            AttackTime = getEnvir()->getTime() + AttackSpeed + 500;
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500; //50 MS per Step

            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::MAC});
            ActionList.push_back(action);

            if (getEnvir()->Random->Next(Settings::PoisonResistWeight) >= getTarget()->Stats[Stat::PoisonResist])
            {
                if (getEnvir()->Random->Next(8) == 0)
                {
                    if (Info->Effect == 0)
                    {
                        Poison *tempVar3 = new Poison(), *this;
                        tempVar3->Owner = this;
                        tempVar3->Duration = 5;
                        tempVar3->PType = PoisonType::Bleeding;
                        tempVar3->TickSpeed = 1000;
                        getTarget()->ApplyPoison(tempVar3, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                    }
                    else if (Info->Effect == 1)
                    {
                        Poison *tempVar4 = new Poison(), *this;
                        tempVar4->Owner = this;
                        tempVar4->Duration = 5;
                        tempVar4->PType = PoisonType::Slow;
                        tempVar4->TickSpeed = 1000;
                        getTarget()->ApplyPoison(tempVar4, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
                    }
                }
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        if (getTarget()->Dead)
        {
            FindTarget();
        }
    }

    void FrostTiger::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (getSitting())
        {
            setSitting(false);
        }
        SitDownTime = NewSitDownTime();

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }

    void FrostTiger::ProcessAI()
    {
        if (!Dead && !getSitting() && getEnvir()->getTime() > SitDownTime)
        {
            setSitting(true);
        }

        MonsterObject::ProcessAI();
    }

    Packet *FrostTiger::GetInfo()
    {
        S::ObjectMonster *tempVar = new S::ObjectMonster();
        tempVar->ObjectID = ObjectID;
        tempVar->Name = getName();
        tempVar->NameColour = NameColour;
        tempVar->Location = getCurrentLocation();
        tempVar->Image = Info->Image;
        tempVar->Direction = getDirection();
        tempVar->Effect = Info->Effect;
        tempVar->AI = Info->AI;
        tempVar->Light = Info->Light;
        tempVar->Dead = Dead;
        tempVar->Skeleton = Harvested;
        tempVar->Poison = CurrentPoison;
        tempVar->Hidden = getHidden();
        tempVar->Extra = getSitting();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
